/**
 * 0040
 *  
 * @Author : roryrywang
 * @DateTime : 2024/8/15 22:28
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> tmp;
    vector<vector<int>> res;

    void dfs(vector<int> &candidates, int target, int sum, int k, vector<bool> &used) {
        if (sum > target)return;
        if (sum == target) {
            res.push_back(tmp);
            return;
        }
        for (int i = k; i < candidates.size(); ++i) {
            if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
                continue;
            }
            tmp.push_back(candidates[i]);
            used[i] = true;
            dfs(candidates, target, sum + candidates[i], i + 1, used);
            tmp.pop_back();
            used[i] = false;
        }
    }

    vector<vector<int>> combinationSum2(vector<int> &candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<bool> used(candidates.size());
        for (int i = 0; i < candidates.size(); ++i) {
            used[i] = false;
        }
        dfs(candidates, target, 0, 0, used);
        return res;
    }
};

int main() {

    return 0;
}